var url = "http://hengzhi.ayvvv.com";
var serCmsData = localStorage.getItem('token');
setTimeout(function () {
    address_lst();
    address();
}, 100);

function address_lst() {
    $.ajax({
        url: url + '/api/user/address/lst',
        dataType: 'json',
        type: 'get',
        data: {
            page: 1,
            limit: 20,
        },
        async: true,
        headers: {
            "x-token": serCmsData
        },
        success: function (data) {
            var JSONobj = JSON.stringify(data);
            if (data.status == 200) {
                retule = JSON.parse(JSONobj);
                var address = "";
                var Arr = retule.data.list;
                Arr.forEach(function (element) {
                    address += `<dl>
                        <dt id="real_name">` + element.real_name + `</dt>
                        <dd>` + element.province + `-` + element.city + `-` + element.district + `-` + element.detail + `</dd>
                        <dd>` + element.phone + `<dd/>
                        <a  id="pno_del" href= "amend.html?amend_x=` + element.address_id + `"> 修改 </a> 
                        <a  id="pno_del" href= "address.html?delll=` + element.address_id + `"> 删除 </a> 
                       
                    </dl>`
                })
                // <a id="def">设为默认地址</a>
                document.getElementById("addid").innerHTML = address;
            } else {
                layer.msg('请登录');
                window.location.href="login.html";
            }
        }
    });
}












function sub() {
    // address();
    var npo = $("#npo").val();
    var select1 = $("#aaappro").val();
    console.log(select1);
    var select2 = $("#urbanizat").val();
    console.log(select2);
    var select3 = $("#district").val();
    console.log(select3);
    var particular = $("#particular").val();
    var phone = $('#phone').val();
    var  is_default= $('#approve').val();

      var data1111 = {
           "real_name": npo,
           "province": select1,
           "city": select2,
           "district": select3,
           "detail": particular,
           "phone": phone,
           'is_default':is_default
       }  
       console.log(data1111)
    $.ajax({
        url: 'http://hengzhi.ayvvv.com/api/user/address/create',
        type: "post",
        data: data1111,
        dataType: 'json',
        headers: {
            "x-token": serCmsData
        },
        success: function (result) {
            console.log(result)
            if (result.status == 200) {

            } else {
            }
        },
    });
}

function address() {
    $.ajax({
        type: "get",
        url: url + "/api/system/city/lst",
        dataType: 'json',
        headers: {
            "x-token": serCmsData
        },
        success: function (result) {
            var JSONobj = JSON.stringify(result);
            // console.log(JSONobj);
            if (result.status == 200) {
                var zz = JSON.parse(JSONobj);
                console.log(zz)
                for (var i = 0; i < zz.data.length; i++) {
                    //  console.log(zz.data[i].merger_name)
                    $('.pproaaa').append(`
             <option value="` +  zz.data[i].name + `" >` + zz.data[i].merger_name + `</option>
             `);
                }
                //二级拼接
                // var sec = $('.pproaaa').val()
                $('#aaappro').click(function () {
                    $('.urbanization').empty();
                    $('.district').empty();
                    var sec = $("#aaappro").find('option:selected').text()
                    //   console.log($("#aaappro").find('option:selected').text()) //第一选中的值

                    for (var i = 0; i < zz.data.length; i++) {
                        if (sec == zz.data[i].merger_name) {
                            for (var j = 0; j < zz.data[i].children.length; j++) {
                                $('.urbanization').append(`
                                <option value="` + zz.data[i].children[j].name + `">` + zz.data[i].children[j].name + `</option>
                             `);
                            }
                        }
                    }
                })
                //三级拼接
                $('#urbanizat').click(function () {
                    $('.district').empty();
                    var sec = $("#urbanizat").find('option:selected').text()
                    // console.log($("#urbanizat").find('option:selected').text()) //第一选中的值
                    // console.log($("#district").find('option:selected').text())
                    for (var i = 0; i < zz.data.length; i++) {
                        for (var j = 0; j < zz.data[i].children.length; j++) {
                            if (sec == zz.data[i].children[j].name) {
                                for (var q = 0; q < zz.data[i].children[j].children.length; q++) {
                                    // console.log(zz.data[i].children[j].children[q])

                                    $('.district').append(`
                                        <option value="` + zz.data[i].children[j].children[q].name + `">` + zz.data[i].children[j].children[q].name + `</option>`);
                                }
                            }
                        }
                    }
                })


            } else {
                layer.msg('网络错误，请先检查网络');
            }
        },
    })
}
